using Indiefreaks.AceOnSteroids.Layers;
using Indiefreaks.Xna.Core;
using Indiefreaks.Xna.Rendering;
using Microsoft.Xna.Framework;
using SynapseGaming.LightingSystem.Rendering;

namespace Indiefreaks.AceOnSteroids
{
    /// <summary>
    /// Renders the initial SunBurn required SplashScreen for Indie & Pro licensees
    /// And our own studio SplashScreen.
    /// </summary>
    public class IntroductionGameState : GameState
    {
        private SunBurnSplashScreen _sunburnSplashScreen;
        private IndiefreaksSplashScreen _indiefreaksStudioSplashScreen;

        /// <summary>
        /// Creates a new instance of the GameState
        /// </summary>
        /// <param name="application">The application instance this GameState will be associated with</param>
        public IntroductionGameState(Application application) : base("Introduction", application)
        {
        }

        #region Overrides of GameState

        /// <summary>
        /// This method is the perfect place for you to initialize your game. It gets called right after you called Application.LoadGameState() but before any Content is 
        /// actually loaded allowing you to state what content should be loaded.
        /// </summary>
        public override void Initialize()
        {
            // we add the IGF SunBurnSplashScreen layer which takes care for you of handling the SunBurn SplashScreen component
            _sunburnSplashScreen = new SunBurnSplashScreen(this, true);
            AddLayer(_sunburnSplashScreen);

            _indiefreaksStudioSplashScreen = new IndiefreaksSplashScreen(this);
            // we set the initial state of the studio splash screen to not Visible so that it firsts renders the SunBurn splash screen
            _indiefreaksStudioSplashScreen.IsVisible = false;
            AddLayer(_indiefreaksStudioSplashScreen);
        }

        /// <summary>
        /// Update called each frame
        /// </summary>
        /// <param name="gameTime"/>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            // we test if the SunBurn SplashScreen ended rendering itself and, if true, we display our studio splash screen
            if (SplashScreen.DisplayComplete)
            {
                _sunburnSplashScreen.IsVisible = false;

                // we test if both splash screens aren't visible to know when to show the studio splash screen
                if (!_sunburnSplashScreen.IsVisible && !_indiefreaksStudioSplashScreen.IsVisible)
                {
                    // the Application class comes with 2 methods to fade in and out from and to a given color in the specified amount of seconds expressed in floats.
                    Application.FadeIn(Color.Black, 1f);
                    _indiefreaksStudioSplashScreen.IsVisible = true;
                    // Both FadeIn/Out methods have an optional third parameter that lets you state how long it should wait (asynchronously) before starting to fade.
                    // Here, we ask it to wait 5 seconds before starting to fade out.
                    Application.FadeOut(Color.Black, 0.25f, 5f);

                    // we need to tell the IntroductionGameState to load the MainMenuGameState when the FadeOut is over so after 6 seconds approxomativelly.
                    // to do so, we'll use the built in Indiefreaks.Xna.Core.Timer static class
                    Timer.Create(6f, false, tick => Application.LoadGameState(new MainMenuGameState(Application)));
                }
            }
        }

        #endregion
    }
}